<?php
	require_once ('scripts.php');
	require_once ('model/dao/vo/VehicleVO.php');
	require_once ('model/dao/vo/SubmissionVO.php');

	$action = $_REQUEST['action'];
	
	if($action == 'deleteImage')
	{
		$del_id = $_POST['delid'];
		
		$selQuery = "SELECT * FROM vehicle_image WHERE image_id =".$del_id;
		$resultSelQuery = mysql_query($selQuery) or die("Query ERROR SELIMG");
		
		$del_img_row =  mysql_fetch_object($resultSelQuery);
		
		unlink('../'.$del_img_row->thump_url); 
		unlink('../'.$del_img_row->image_url); 
		unlink('../'.$del_img_row->crop_url); 
		
		$delQuery = "DELETE FROM vehicle_image WHERE image_id =".$del_id;
		$resultDelQuery = mysql_query($delQuery) or die("Query ERROR DELIMG");
		
		echo true;
	}
	
	if($action == 'create')
	{ 
        $submit_type=$_REQUEST['submit_type'];
		$object=new VehicleVO();	    
	    if($submit_type == 0)
	    {
			$pnotesarray=(isset($_POST['privatenotesid']))?$_POST['privatenotesid']:null;		
			$object->year = $_POST['year'];
			$object->make=$_POST['make'];
			$object->model=$_POST['model'];
			$object->vin=$_POST['vin'];
			$object->mileage=$_POST['mileage'];
			$object->engine=$_POST['engine'];
			$object->transmission=$_POST['transmission'];
			$object->drivetrain_type=$_POST['drivetrain_type'];
			$object->title_note=$_POST['title_note'];
			$object->title_name=$_POST['title_name'];
			$object->ad_source=$_POST['ad_source'];
			$object->wrong=$_POST['wrong'];
			$object->great=$_POST['great'];
			$object->paper_work=$_POST['paper_work'];		
			$object->i_want_to=$_POST['i_want_to'];
			$object->first_name=$_POST['first_name'];
			$object->bussiness_name=$_POST['bussiness_name'];
			$object->contact_name=$_POST['contact_name'];
			$object->street_address=$_POST['street_address'];
			$object->city=$_POST['city'];
			$object->state=$_POST['state'];
			$object->zip=$_POST['zip'];
			$object->phone=$_POST['phone'];
			$object->cell_phone=$_POST['cell_phone'];
			$object->email=$_POST['email'];
			$object->contact_me=$_POST['contact_me'];
			$object->contact_note=$_POST['contact_note'];
			$object->delivery=$_POST['delivery'];
			$object->removal_date=$_POST['date'];
			$object->color=$_POST['color'];
			$object->delivery_note=$_POST['delivery_note'];
			$object->accepted_price=$_POST['accepted_price'];
			$object->asking_price=$_POST['asking_price'];
			$object->bid_price=$_POST['bid_price'];
			$object->v_street_address=$_POST['v_street_address'];
			$object->v_city=$_POST['v_city'];
			$object->v_state=$_POST['v_state'];
			$object->v_zip=$_POST['v_zip'];
			//$object->located_at=$_POST['located_at'];
			$object->i_want_to=$_POST['i_want_to'];
			$object->is_watch=0;
			$object->is_read=0;		
			$object->status='New Submission';
			$object->current_action='New Submission';
			$object->is_partner=0;
			//$object->locked_id=0;
			$object->adminUser->firstName =$_REQUEST['firstname'];
			$object->adminUser->adminId =$_REQUEST['adminid'];
			$object->assist_by =$_REQUEST['firstname'];
			//$object->adminUser->adminId =$_REQUEST['adminid'];
			createNewVehicle($object);
		}
        else
       {
          $object->year = $_REQUEST['year'];
			$object->make=$_REQUEST['make'];
			$object->model=$_REQUEST['model'];
			$object->transmission=$_REQUEST['transmission'];
			$object->first_name=$_REQUEST['first_name'];
			$object->phone=$_REQUEST['phone'];
			$object->email=$_REQUEST['email'];
			$object->street_address=$_REQUEST['street_address'];
			$object->city=$_REQUEST['city'];
			$object->state=$_REQUEST['state'];
			$object->asking_price=$_REQUEST['asking_price'];
			$object->bid_price=0;
			$object->is_watch=0;
			$object->is_read=0;
			$object->accepted_price=0;
			$object->status='New Submission';
			$object->current_action='New Submission';
			$object->is_partner=0;	    
			$object->great = $_REQUEST['great'];
			$object->wrong=$_REQUEST['wrong'];
			$object->win=$_REQUEST['vin'];
			$object->mileage=$_REQUEST['mileage'];
			$object->drivetrain_type=$_REQUEST['drivetrain_type'];
			$object->engine=$_REQUEST['engine'];
			$object->ad_source=$_REQUEST['ad_source'];
			//$object->contact_name=$_REQUEST['contact_name'];
			$object->paper_work=$_REQUEST['paper_work'];
			$object->located_at=$_REQUEST['located_at'];
			$object->i_want_to=$_REQUEST['i_want_to'];
			$object->removal_date=$_REQUEST['removal_date'];
			createNewVehicle($object);
		}
	}
	if($action == 'createbyadmin')
	{ 
        
		$object=new VehicleVO();	    
	  
			$object->private_note_array=(isset($_POST['privatenotesid']))?$_POST['privatenotesid']:null;		
			$object->year = $_POST['year'];
			$object->make=$_POST['make'];
			$object->model=$_POST['model'];
			$object->vin=$_POST['vin'];
			$object->mileage=$_POST['mileage'];
			$object->engine=$_POST['engine'];
			$object->transmission=$_POST['transmission'];
			$object->drivetrain_type=$_POST['drivetrain_type'];
			$object->title_note=$_POST['title_note'];
			$object->title_name=$_POST['title_name'];
			$object->ad_source=$_POST['ad_source'];
			$object->wrong=$_POST['wrong'];
			$object->great=$_POST['great'];
			$object->paper_work=$_POST['paper_work'];		
			$object->i_want_to=$_POST['i_want_to'];
			$object->first_name=$_POST['first_name'];
			$object->bussiness_name=$_POST['bussiness_name'];
			$object->contact_name=$_POST['contact_name'];
			$object->street_address=$_POST['street_address'];
			$object->city=$_POST['city'];
			$object->state=$_POST['state'];
			$object->zip=$_POST['zip'];
			$object->phone=$_POST['phone'];
			$object->cell_phone=$_POST['cell_phone'];
			$object->email=$_POST['email'];
			$object->contact_me=$_POST['contact_me'];
			$object->contact_note=$_POST['contact_note'];
			$object->delivery=$_POST['delivery'];
			$object->removal_date=$_POST['date'];
			$object->color=$_POST['color'];
			$object->delivery_note=$_POST['delivery_note'];
			$object->accepted_price=$_POST['accepted_price'];
			$object->asking_price=$_POST['asking_price'];
			$object->bid_price=$_POST['bid_price'];
			$object->v_street_address=$_POST['v_street_address'];
			$object->v_city=$_POST['v_city'];
			$object->v_state=$_POST['v_state'];
			$object->v_zip=$_POST['v_zip'];
			//$object->located_at=$_POST['located_at'];
			$object->i_want_to=$_POST['i_want_to'];
			$object->is_watch=0;
			$object->is_read=0;		
			$object->status='New Submission';
			$object->current_action='New Submission';
			$object->is_partner=0;
			$object->adminUser->firstName =$_POST['fadminname'];
			$object->adminUser->admin_id =$_POST['admin_id'];
			$object->assist_by =$_POST['fadminname'];
			$object->sub_report->sub_ip='';				
			$object->sub_report->sub_ip = '';
			$object->sub_report->latitude = '';
			$object->sub_report->longtitude = '';
			$object->sub_report->via_m_buyer = 0;
			$object->sub_report->sub_platform = 'Windows Web Application';
			$object->sub_report->sub_device = 'Personal Machine';		
			$object->sub_report->via_w_seller = 0;
			$object->sub_report->via_w_buyer = 1;
			$object->sub_report->via_m_seller = 0;
			$result = VehicleDAOImpl::getInstance()->createNewVehicle($object);
		echo $result;
		
		
	}
	
	function createNewVehicle($vehicle) 
	{   
		date_default_timezone_set('America/Los_Angeles');
		$myDate = date('Y-m-d H:i:s');
		$o_date = date('Y-m-d');
		$submissionVO = new SubmissionVO();
		$submissionVO->sub_ip = '';
		$submissionVO->latitude = '';
		$submissionVO->longtitude = '';
		if($_REQUEST['submit_type'] == 0)
		{
		$adminUser = $vehicle->adminUser->firstName;
		$adminId = $vehicle->adminUser->adminId;
		$submissionVO->via_m_buyer = 1;
		$submissionVO->sub_platform = 'Mobile OS';
		$submissionVO->sub_device = 'Mobile';		
		$submissionVO->via_w_seller = 0;
		$submissionVO->via_w_buyer = 0;
		$submissionVO->via_m_seller = 0;
		$type = 'BuyerSubmit';
		$created_by = $vehicle->adminUser->firstName;
		
        }
		else
		{
		$adminUser ='';
		$adminId =null ;
		$submissionVO->via_m_buyer = 0;
		$submissionVO->sub_platform = 'Windows Web Application';
		$submissionVO->sub_device = 'Personal Machine';		
		$submissionVO->via_w_seller = 1;
		$submissionVO->via_w_buyer = 0;
		$submissionVO->via_m_seller = 0;
		$type = 'SellerSubmit';
		$created_by = $vehicle->first_name;
		}
                $delivery_id='';
		  if(isset($vehicle->delivery))
                $delivery = mysql_fetch_object(mysql_query("SELECT id FROM company WHERE company_name='".$vehicle->delivery."'"));
                 if(isset($delivery->id))
                  $delivery_id= $delivery->id;
                
  		$email = $vehicle->email;
  		$vid = $vehicle->vehicle_id;		
  		$query_insert = "INSERT INTO vehicle ( `vehicle_id` , `year` , `make` , `model` , `vin` , `mileage` , `engine` , `transmission` , `drivetrain_type` , `title_note` , `title_name` , `ad_source` , `color` , `wrong` , `great` , `i_want_to` , `bid_price` , `asking_price`, `accepted_price` , `removal_date` , `first_name` , `email` , `phone` , `cell_phone` , `contact_me` , `located_at` , `paper_work` , `street_address` , `city` , `state` , `zip` , `v_street_address` , `v_city` , `v_state` , `v_zip` , `contact_name` , `bussiness_name` , `contact_phone` , `delivery` , `delivery_note` , `delivery_option` , `private_note` , `contact_note` , `is_watch` , `is_read` , `status` , `current_action` ,`locked_by`,`locked_id`,`assist_by`,`locked_date`, `update_date` , `date`, `created_by`, `is_partner` ) VALUES (NULL,'".$vehicle->year. "','" .addslashes($vehicle->make). "','" .addslashes($vehicle->model). "','" .addslashes($vehicle->vin). "','" .addslashes($vehicle->mileage). "','" .addslashes($vehicle->engine). "','" .addslashes($vehicle->transmission). "','" .addslashes($vehicle->drivetrain_type). "','" .addslashes($vehicle->title_note). "','" .addslashes($vehicle->title_name). "','" .addslashes($vehicle->ad_source). "','" .addslashes($vehicle->color). "','" .addslashes($vehicle->wrong). "','" .addslashes($vehicle->great). "','" .addslashes($vehicle->i_want_to). "',".$vehicle->bid_price.",".$vehicle->asking_price.",".$vehicle->accepted_price.",'" .$vehicle->removal_date. "','" .addslashes($vehicle->first_name). "','" .addslashes($vehicle->email). "','" .$vehicle->phone. "','" .$vehicle->cell_phone. "','" .addslashes($vehicle->contact_me). "','" .addslashes($vehicle->located_at). "','" .addslashes($vehicle->paper_work). "','" .addslashes($vehicle->street_address). "','" .addslashes($vehicle->city). "','" .addslashes($vehicle->state). "','" .addslashes($vehicle->zip). "','" .addslashes($vehicle->v_street_address). "','" .addslashes($vehicle->v_city). "','" .addslashes($vehicle->v_state). "','" .addslashes($vehicle->v_zip). "','" .addslashes($vehicle->contact_name). "','" .addslashes($vehicle->bussiness_name). "','" .$vehicle->contact_phone. "','".$delivery_id."','" .addslashes($vehicle->delivery_note). "','" .addslashes($vehicle->delivery_option). "','" .addslashes($vehicle->private_note). "','" .addslashes($vehicle->contact_note). "'," .$vehicle->is_watch. ",".$vehicle->is_read.",'" .addslashes($vehicle->status). "','" .addslashes($vehicle->current_action). "','" .addslashes($vehicle->locked_by). "',0,'" .addslashes($adminUser). "','" .$myDate."','" .$myDate."','" .$myDate."','".$created_by."', ".$vehicle->is_partner.")";
		$result_insert = mysql_query($query_insert) or die("Query ERROR9".$query_insert);
		$lastInsertedID = mysql_insert_id();	
                 if($result_insert)
                      mysql_query("INSERT INTO vehicle_tow_status (vehicle_id,tower_id,tow_status,assigned_by,assigned_date) VALUES(".$lastInsertedID.", '" . $delivery_id. "', 1, ".$adminId.", NOW())");
                 
		$submission_insert = "INSERT INTO submission_tracker (`vehicle_id`,`sub_ip`,`sub_platform`,`sub_device`,`gps_latitude`,`gps_longtitude`,`via_web_seller`,
		`via_web_buyer`,`via_mobile_seller`,`via_mobile_buyer`,`date`) VALUES (".$lastInsertedID.",'".$submissionVO->sub_ip."','".$submissionVO->sub_platform."','".$submissionVO->sub_device."','".$submissionVO->latitude."','".$submissionVO->longtitude."',".$submissionVO->via_w_seller.",".$submissionVO->via_w_buyer.",".$submissionVO->via_m_seller.",".$submissionVO->via_m_buyer.",'".$o_date."')";
		$submission_result = mysql_query($submission_insert) or die("Query ERROR111".$submission_insert);	
		$vehicle->vehicle_id = $lastInsertedID;
		VehicleDAOImpl::getInstance()->emailNotificationSender($vehicle, $type, $adminUser, $lastInsertedID);
		if($adminUser!=null && $adminId!=null)
		{	if (isset($_POST['privatenotesid']))
			{
				$pnotesarray=$_POST['privatenotesid'];		
				if(count($pnotesarray)>0)
				{
					$pnotestring='';
					for($i=0;$i<count($pnotesarray);$i++)
					{
						$pnotestring.=$pnotesarray[$i];
						if($i!=count($pnotesarray)-1)
						$pnotestring.=',';
					}
					$queryupdate="UPDATE private_notes SET vehicle_id=".$lastInsertedID."  where note_id IN(".$pnotestring.")";
					$result = mysql_query($queryupdate) or die("Query ERROR9".$queryupdate);
				}
			}
			echo $lastInsertedID;
		}
		else
		{
		echo $lastInsertedID;

		}
		if($_REQUEST['submit_type'] == 1)
		{
		$sub_ray = 'Error';
		if ($_POST['make'] != '' && $_POST['model'] != '' && $_POST['year'] != '' && $_POST['year'] != 0)
		{
			$sub_ray = 'Success';
		}
		$myFile = "log.txt";
		$fh = fopen($myFile, 'a') or die("can't open file");
		$stringData = $sub_ray.":: Vehicle ID:".$lastInsertedID."---- Browser Name:".$_POST['bname']."---- Browser Version:".$_POST['bver']."\n";
		fwrite($fh, $stringData);
		fclose($fh);
		}
		if (isset($_POST['image_id']))
		{	
		$imageid=$_POST['image_id'];
		/*if(count($imageid)>0)
				{
					for ($i=0; $i< count($imageid); $i++)
					{
						$image_insert = "UPDATE vehicle_image SET vehicle_id='".$lastInsertedID."' WHERE image_id='".$imageid[$i]."'";
						$image_insert = mysql_query($image_insert) or die("Query ERROR10");                  
					}
				  
				}*/
			if($imageid != '')
			{
				 $image_insert_1 = "UPDATE vehicle_image SET vehicle_id='".$lastInsertedID."' WHERE image_id IN(".$imageid .")";
					 $image_insert = mysql_query($image_insert_1) or die("Query ERROR10");                  
			}
		}

	}
	function encodeJsonString($vehicle)
    {  
		$reminder_1 = array();
		if($vehicle->reminder)
		{  
			$reminder_1[]=array("watchid"=> $vehicle->reminder->watch_id,
			 "vehicle_id"=> $vehicle->reminder->vehicle_id,
			 "set_by"=> $vehicle->reminder->set_by,
			 "set_date_time"=> $vehicle->reminder->set_date,
			 "to_date"=> $vehicle->reminder->to_date,
			 "remind_note"=> $vehicle->reminder->remind_note,
			 "first_name"=> $vehicle->reminder->remind_by
			 );
		}
		$ar_images = $vehicle->image_collection->source;
		$images = array();
		for($i=0; $i<count($ar_images); $i++)
		{
			$images[] = array(
			"imageId"=>$ar_images[$i]->imageId,
			"imageUrl"=>$ar_images[$i]->imageUrl,
			"thumpUrl"=>$ar_images[$i]->thumpUrl);
		}
		
		$ar_message = $vehicle->messages->source;
		$messages = array();
		for($i=0; $i<count($ar_message); $i++)
		{
			if($ar_message[$i]!='')
			{
			  $messages[] = array(
			   "vehicleId" => $ar_message[$i]->vehicleId,
			   "message" => $ar_message[$i]->message,
			   "messageOwener" => $ar_message[$i]->messageOwener,
			   "messageRecipient" => $ar_message[$i]->messageRecipient,
			   "offer" => $ar_message[$i]->offer,
			   "isRead" => $ar_message[$i]->isRead,
			   "dateTime" => $ar_message[$i]->dateTime);
			 }
		}		
		$ar_privatenotes = $vehicle->privateNote_collection->source;
		$pnotes = array();
		for($i=0; $i<count($ar_privatenotes); $i++)
		{
				
			if($ar_privatenotes[$i]!='')
			{
			  $pnotes[] = array(
			   "vehicleId" => $ar_privatenotes[$i]->vehicleId,
			   "message" => $ar_privatenotes[$i]->message,
			   "messageOwener" => $ar_privatenotes[$i]->messageOwener,
			   "messageRecipient" => $ar_privatenotes[$i]->messageRecipient,
			   "offer" => $ar_privatenotes[$i]->offer,
			   "isRead" => $ar_privatenotes[$i]->isRead,
			   "dateTime" => $ar_privatenotes[$i]->dateTime);
			 }
		}
		
		$vehicles[] = array(
		
		"vehicle_id" => $vehicle->vehicle_id,
        "year"=> $vehicle->year,
        "make"=> $vehicle->make,
        "model"=> $vehicle->model,
        "vin"=> $vehicle->vin,
        "mileage"=> $vehicle->mileage,
        "engine"=> $vehicle->engine,
        "transmission"=> $vehicle->transmission,
        "drivetrain_type"=> $vehicle->drivetrain_type,
        "title_note"=> $vehicle->title_note,
        "wrong"=> $vehicle->wrong,
        "great"=> $vehicle->great,
        "i_want_to"=> $vehicle->i_want_to,
        "bid_price"=> $vehicle->bid_price,
        "asking_price"=> $vehicle->asking_price,
        "accepted_price"=> $vehicle->accepted_price,
        "removal_date"=> $vehicle->removal_date,
        "first_name"=> $vehicle->first_name,
        "email"=> $vehicle->email,
        "phone"=> $vehicle->phone,
        "cell_phone"=> $vehicle->cell_phone,
        "contact_me"=> $vehicle->contact_me,
        "located_at"=> $vehicle->located_at,
        "paper_work"=> $vehicle->paper_work,
        "street_address"=> $vehicle->street_address,
        "city"=> $vehicle->city,
        "state"=> $vehicle->state,
        "zip"=> $vehicle->zip,
        "v_street_address"=> $vehicle->v_street_address,
        "v_city"=> $vehicle->v_city,
        "v_state"=> $vehicle->v_state,
        "v_zip"=> $vehicle->v_zip,
        "contact_name"=> $vehicle->contact_name,
        "contact_phone"=> $vehicle->contact_phone,
        "delivery"=> $vehicle->delivery,
        "delivery_note"=> $vehicle->delivery_note,
        "delivery_option"=> $vehicle->delivery_option,
        "is_read"=>$vehicle->is_read,
		"is_watch"=>$vehicle->is_watch,
        "private_note"=> $vehicle->private_note,
        "contact_note"=> $vehicle->contact_note,
   
        "status"=> $vehicle->status,
        "current_action"=> $vehicle->current_action,
        "update_date"=> $vehicle->update_date,
        "date"=> $vehicle->date,
       
       
        "title_name"=> $vehicle->title_name,
        "ad_source"=> $vehicle->ad_source,
        "color"=> $vehicle->color,
        "bussiness_name"=> $vehicle->bussiness_name,
		 "messages"=> $messages, 
		 "images"=> $images,
		 "pnotes"=> $pnotes ,
		  "reminder_1"=>$reminder_1
        );

	   $vehicledata = array("vehicle"=>$vehicles);

       return $vehicledata ;
    }
	?>